﻿/* Author: 
	Marek Pietrzak
*/

// loading map using Leaflet, centering it on Wrocław

var map = new L.Map('map');

var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/fc5f315874da4bdf8f0a72bc1376147e/997/256/{z}/{x}/{y}.png',
    cloudmadeAttrib = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
    cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttrib});
	
var wroclaw = new L.LatLng(51.110598, 17.032859); // geographical point (longitude and latitude)
map.setView(wroclaw, 13).addLayer(cloudmade);


// map settings

map.on('click', onMapClick);
        
var popup = new L.Popup();
        
function onMapClick(e) {
    var latlngStr = '(' + e.latlng.lat.toFixed(3) + ', ' + e.latlng.lng.toFixed(3) + ')';
            
    popup.setLatLng(e.latlng);
    popup.setContent("You clicked the map at " + latlngStr);
    
    map.openPopup(popup);
}


// universities layer

var pwrMarker = new L.Marker(new L.LatLng(51.108, 17.062)).bindPopup("Politechnika Wrocławska"),
	uwrMarker = new L.Marker(new L.LatLng(51.114, 17.034)).bindPopup("Uniwersytet Wrocławski"),
	ueMarker = new L.Marker(new L.LatLng(51.091, 17.026)).bindPopup("Uniwersytet Ekonomiczny");
	
var uniLayer = new L.LayerGroup();

uniLayer.addLayer(pwrMarker)
        .addLayer(uwrMarker)
        .addLayer(ueMarker);

map.addLayer(uniLayer);


// toggling view of the filters tab

$('button#showFilters').click(function(){
	$('section#filters > form').slideToggle(200);
	if ($('button#showFilters').text() == 'Ukryj opcje wyszukiwania') {
		$('button#showFilters').text('Pokaż opcje wyszukiwania');
	}
	else {
		$('button#showFilters').text('Ukryj opcje wyszukiwania');
	}
});

// toggling view of the adding tab

$('button#adding').click(function(){
	$('section#adding > form').slideToggle(200);
});

$('#adding > form').submit(function(){
	var title = $('#title').attr('value');
	var street = $('#street').attr('value');
	var price = $('#price').attr('value');
	var text= $('#text').attr('value');
	$.ajax({
		type: "POST",
		url: "add.php",
		data: {title:title, street:street, price:price, text:text},
		success: function(){
			$('#adding > form').hide(function(){
				$('div.success').fadeIn();
			});
		}
	});
});

// showing content on anchor click 

var toggleList = function(){
	$('#adList a').each(function(){
		$(this).click(function(){
			var id = $(this).find('span.adId').text();
			$('article#ad-content').load("adLoad.php", {adId: id});
			$('#side').animate({
				opacity: 0
				}, 200, function(){
					$(this).addClass('hidden'); 
					$('#content').removeClass('hidden').animate({
						opacity: 1
					}, 200);
				}
			);
		});
	});
}

// returning to the list from content

$('button#listReturn').click(function(){
	$('#content').animate({
		opacity: 0
		}, 200, function(){
			$(this).addClass('hidden'); 
			$('#side').removeClass('hidden').animate({
				opacity: 1
			}, 200);
		}
	);
});

var interactiveList = function(){
	var options = {
		valueNames: [ 'streetName', 'priceLimit' ]
	};

	var hackerList = new List('side', options);
}

// loading ad list into <ul> and running the declared functions

$(document).ready(function()
    {
        $("section#adList").load("adList.php", function() {
			interactiveList();
			toggleList();
		});
    });
